<?php
class Dnevnik
{
    protected $profil;
    protected $user;
    protected $ucenik;

    public function Dnevnik($ucenik)
    {
        $this->user = sfContext::getInstance()->getUser();
        $this->profil = DbFinder::from('Profil')->findPk($this->user->getAttribute('id'));
        $this->ucenik = $ucenik;
    }

    public function getPredmetiUcenika()
    {
        $predmet_smers = DbFinder::from('PredmetSmer')->join('Smer')->leftJoin('PredmetSmer.PredmetId', 'Predmet.Id')->leftJoin('Razred.Id', 'Smer.Id')->withColumn('Predmet.Id', 'PredmetId')->withColumn('Predmet.PredmetKratki', 'PredmetKratki')->withColumn('Predmet.PredmetNaziv', 'PredmetNaziv')->withColumn('Predmet.Tip', 'PredmetTip')->where('Razred.Id', $this->ucenik->getRazredId())->find();

        // Predmeti ucenika
        $predmets = array();
        foreach($predmet_smers as $predmet_smer)
        {
          $predmets[$predmet_smer->getColumn('PredmetId')] = array($predmet_smer->getColumn('PredmetNaziv'), $predmet_smer->getColumn('PredmetTip'));
        }

        // Izborni predmeti ucenika
        $smers = array();
        $grupas = $this->ucenik->getUcenikRazreds();
        foreach($grupas as $grupa)
        {
          $smer = $grupa->getRazred()->getSmer();
          if($smer->getTip() != SmerPeer::$tip['grupni'])
            continue;
          $smers[] = $smer->getNaziv();

        }
        $izborni_arrs = DbFinder::from('Predmet')->where('Predmet', 'in', $smers)->find();
        $izbornis = array();
        foreach($izborni_arrs as $izborni_arr)
          $izbornis[$izborni_arr->getTip()] = array($izborni_arr->getId(), $izborni_arr->getPredmetNaziv());

        return array($predmets, $izbornis, $grupas);
    }

    // svi predmeti ucenika u nizu
    public function getPredmetiAll()
    {
        $predmet_smers = DbFinder::from('PredmetSmer')->join('Smer')->leftJoin('PredmetSmer.PredmetId', 'Predmet.Id')->leftJoin('Razred.Id', 'Smer.Id')->withColumn('Predmet.Id', 'PredmetId')->withColumn('Predmet.PredmetKratki', 'PredmetKratki')->withColumn('Predmet.PredmetNaziv', 'PredmetNaziv')->where('Predmet.Tip', '<>', PredmetPeer::$tip['placeholder'])->where('Razred.Id', $this->ucenik->getRazredId())->find();

        // Predmeti ucenika
        $predmets = array();
        foreach($predmet_smers as $predmet_smer)
        {
          $predmets[$predmet_smer->getColumn('PredmetId')] = $predmet_smer->getColumn('PredmetKratki');
        }

        // Izborni predmeti ucenika
        $smers = array();
        $grupas = $this->ucenik->getUcenikRazreds();
        foreach($grupas as $grupa)
        {
          $smer = $grupa->getRazred()->getSmer();
          if($smer->getTip() != SmerPeer::$tip['grupni'])
            continue;
          $smers[] = $smer->getNaziv();

        }
        $izborni_arrs = DbFinder::from('Predmet')->where('Predmet', 'in', $smers)->find();
        foreach($izborni_arrs as $izborni_arr)
          $predmets[$izborni_arr->getId()] = $izborni_arr->getPredmetKratki();

        return $predmets;
    }

    /**
    * sve ocene u niz $ocenas
    *
    */
    public function getUcenikOcenas()
    {
        $ocenas = array();
        $ocene_ar = DbFinder::from('Ocena')->where('RazredId', $this->ucenik->getRazredId())->where('UcenikId', $this->ucenik->getId())->find();
        foreach($ocene_ar as $ocena)
        {
          $ocenas[$ocena->getPredmetId()][$ocena->getPeriod()][] = array($ocena->getId(), $ocena->getOcena(), str_replace("'", "`", str_replace('"', '`', $ocena->getOpis())));
        }

        return $ocenas;
    }

    public function getUcenikZakljucnas()
    {
        $ocenas = array();
        $ocene_ar = DbFinder::from('Zakljucna')->where('RazredId', $this->ucenik->getRazredId())->where('UcenikId', $this->ucenik->getId())->find();
        foreach($ocene_ar as $ocena)
        {
          $ocenas[$ocena->getPredmetId()][$ocena->getPeriod()][] = array($ocena->getId(), $ocena->getOcena(), $ocena->getOpis());
        }

        return $ocenas;
    }

    public function getUcenikIzostanaks($ucenik_id)
    {
       $opr_1 = DbFinder::from('Izostanak')->select('Sum')->withColumn('count(Id)', 'Sum')->where('UcenikId', $ucenik_id)->where('Datum', '<', '2010-01-23')->where('Opravdan', '1')->findOne();
       $neopr_1 = DbFinder::from('Izostanak')->select('Sum')->withColumn('count(Id)', 'Sum')->where('UcenikId', $ucenik_id)->where('Datum', '<', '2010-01-23')->where('Opravdan', '0')->findOne();
       $opr_2 = DbFinder::from('Izostanak')->select('Sum')->withColumn('count(Id)', 'Sum')->where('UcenikId', $ucenik_id)->where('Opravdan', '1')->findOne();
       $neopr_2 = DbFinder::from('Izostanak')->select('Sum')->withColumn('count(Id)', 'Sum')->where('UcenikId', $ucenik_id)->where('Opravdan', '0')->findOne();
       return array($opr_1, $neopr_1, $opr_2, $neopr_2);
    }

    /**
    * sve ocene razreda iz predmeta u niz $ocenas
    *
    */
    public static function getRazredPredmetOcenas($razred_id, $predmet_id)
    {
        $ocenas = array();
        $ocene_ar = DbFinder::from('Ocena')->where('RazredId', $razred_id)->where('PredmetId', $predmet_id)->find();
        foreach($ocene_ar as $ocena)
        {
          $ocenas[$ocena->getUcenikId()][$ocena->getPeriod()][] = array($ocena->getId(), $ocena->getOcena(), $ocena->getOpis());
        }

        return $ocenas;
    }

    public static function getGrupaPredmetOcenas($razred_id, $predmet_id)
    {
        $uceniks = DbFinder::from('Ucenik')->select('Id')->join('UcenikRazred')->where('UcenikRazred.RazredId', $razred_id)->find();
        $ocenas = array();
        $ocene_ar = DbFinder::from('Ocena')->where('UcenikId', 'in', $uceniks)->where('PredmetId', $predmet_id)->find();
        foreach($ocene_ar as $ocena)
        {
          $ocenas[$ocena->getUcenikId()][$ocena->getPeriod()][] = array($ocena->getId(), $ocena->getOcena(), $ocena->getOpis());
        }

        return $ocenas;
    }

    public function getTemplate()
    {
      switch($this->ucenik->getRazred()->getSmer()->getNaziv())
      {
          case '1':
            return 'OsnovnaPrvi';
          case '2-4':
            return 'OsnovnaDrugi';
          case '5-7':
            return 'OsnovnaPeti';
          case '8':
            return 'OsnovnaOsmi';
          default:
            return (0);
      }
    }

  public static function oceneGraf($ocena_ars, $razred_id, $grupa_id=0, $edit=0)
  {
    $print = "";
    foreach($ocena_ars as $ocena_ar)
    {
        if(isset($ocena_ar[2]) and $ocena_ar[2] != '')
        {
            list($id, $ocena, $opis) = $ocena_ar;
            $class = 'ocena_obrazlozena tooltip';
        }
        else
        {
            list($id, $ocena) = $ocena_ar;
            $opis = '';
            $class = '';
        }
          if($edit)
          {
            $print .= '<a class=\'ocena '.$class.'\' id=\'ocena_id_'.$id.'\' href=\'javascript:void(0)\' title=\''.wordwrap($opis, 60, '<br />').'\' onclick=\'setContent = new Boxy(
                  "<div>';
            $print .= '<label class=\"label_ocena\">Unos ocene</label><br /><input type=\"text\" id=\"ocena_'.$id.'\" value=\"'.$ocena.'\"><br />';
            $print .= '<label class=\"label_ocena\">Upišite obrazloženje za unetu ocenu</label><br /><textarea id=\"opis_'.$id.'\">'.$opis.'</textarea><br />';
            $print .= '<a id=\"dugme_'.$id.'\" class=\"sb\" href=\"javascript: void(0)\" onclick=\"promeniOcenu('.$id.','.$razred_id.','.$grupa_id.'); setContent.unload();\"><span>Unesi</span></a>';
            $print .= '</div>", { title: "Promeni ocenu", closeText: "Zatvori"}); $("#ocena_'.$id.'").focus();\'>'.$ocena.'</a>';
          }
          else
          {
              $print .= '<a class=\'ocena '.$class.'\' href=\'javascript:void(0)\' title=\''.wordwrap($opis, 60, '<br />').'\'>'.$ocena.'</a>';
          }
    }

    return $print;
  }

  public static function novaZakljucnaGraf($ucenik_id, $razred_id, $grupa_id, $predmet_id, $predmet_naziv, $period)
  {
      $print = '';
      $print .= '<a id=\'nova_zak_'.$predmet_id.'_'.$period.'\' href=\'javascript:void(0)\' class=\'nova\'
                onclick=\'setContent = new Boxy(
                  "<div>';
      $print .= '<label class=\"label_ocena\">Unos zakljucne ocene</label><br /><input type=\"text\" id=\"ocena_zak_'.$predmet_id.'_'.$period.'\"><br />';
      $print .= '<label class=\"label_ocena\">Upišite obrazloženje za unetu ocenu</label><br /><textarea id=\"opis_zak_'.$predmet_id.'_'.$period.'\" /><br />';
      $print .= '<a id=\"dugme_zak_'.$predmet_id.'_'.$period.'\" class=\"sb\" href=\"javascript: void(0)\" onclick=\"unesiZakljucnuOcenu('.$ucenik_id.','.$razred_id.','.$grupa_id.','.$predmet_id.','.$period.'); setContent.unload();\"><span>Unesi</span></a>';
      $print .= '</div>", { title: "'.$predmet_naziv.'", closeText: "Zatvori"}); $("#ocena_zak_'.$predmet_id.'_'.$period.'").focus();\'>
                <img src=\'/images/unos.gif\' alt=\'nova\' title=\'zakljucna ocena\'></a>';
      return $print;
  }

  public static function zakljucnaGraf($zak_ars, $razred_id, $grupa_id=0, $edit=0)
  {
        $print = "";

        $zak_txt = array('nedovoljan', 'dovoljan', 'dobar', 'vrlodobar', 'odličan');
        $zak_ar = current($zak_ars);
        if(isset($zak_ar[2]) and $zak_ar[2] != '')
        {
            list($id, $zak, $opis) = $zak_ar;
            if($opis != '')
              $zak_str = mb_substr($opis, 0, 3, 'UTF-8');
            else $zak_str = $zak;
            $class = 'ocena_obrazlozena tooltip';

            /*if($edit)
            {
              $print .= '<a class=\'ocena '.$class.'\' id=\'zak_id_'.$id.'\' href=\'javascript:void(0)\' title=\''.wordwrap($opis, 60, '<br />').'\' onclick=\'setContent = new Boxy(
                  "<div>';
              $print .= '<label class=\"label_ocena\">Nivo ocene</label><br /><input type=\"text\" id=\"zak_'.$id.'\" value=\"'.$zak.'\"><br />';
              $print .= '<label class=\"label_ocena\">Upišite opisnu ocenu</label><br /><textarea id=\"opis_'.$id.'\">'.$opis.'</textarea><br />';
              $print .= '<a id=\"dugme_'.$id.'\" class=\"sb\" href=\"javascript: void(0)\" onclick=\"promeniOpisnuZakljucnu('.$id.','.$razred_id.','.$grupa_id.'); setContent.unload();\"><span>Unesi</span></a>';
              $print .= '</div>", { title: "Promeni ocenu", closeText: "Zatvori"}); $("#zak_'.$id.'").focus();\'>'.$zak_str.'</a>';
            }
            else
            {*/
              $print .= '<a class=\'ocena '.$class.'\' href=\'javascript:void(0)\' title=\''.wordwrap($opis, 60, '<br />').'\'>'.$zak_txt[$zak - 1].' '.$zak.'</a>';
            //}
        }
        else
        {
            list($id, $zak) = $zak_ar;
            $opis = '';
            $class = '';
            /*if($edit)
            {
              $print .= '<a class=\'ocena '.$class.'\' id=\'zak_id_'.$id.'\' href=\'javascript:void(0)\' title=\''.wordwrap($opis, 60, '<br />').'\' onclick=\'setContent = new Boxy(
                  "<div>';
              $print .= '<label class=\"label_zak\">Unos ocene</label><br /><input type=\"text\" id=\"zak_'.$id.'\" value=\"'.$zak.'\"><br />';
              $print .= '<a id=\"dugme_'.$id.'\" class=\"sb\" href=\"javascript: void(0)\" onclick=\"promeniZakljucnu('.$id.','.$razred_id.','.$grupa_id.'); setContent.unload();\"><span>Unesi</span></a>';
              $print .= '</div>", { title: "Promeni ocenu", closeText: "Zatvori"}); $("#zak_'.$id.'").focus();\'>'.$zak.'</a>';
            }
            else
            {*/
              $print .= '<a class=\'ocena '.$class.'\' href=\'javascript:void(0)\' title=\''.wordwrap($opis, 60, '<br />').'\'>'.$zak_txt[$zak - 1].' '.$zak.'</a>';
            //}

        }

    return $print;
  }

  public static function vladanjeGraf($zak_ars, $razred_id, $grupa_id=0, $edit=0)
  {
     if($zak_ars == 0)
     {
         $opis = '';
         $class = '';
         if($edit)
         {
           $print .= '<a class=\'ocena '.$class.'\' id=\'zak_id_'.$id.'\' href=\'javascript:void(0)\' title=\''.wordwrap($opis, 60, '<br />').'\' onclick=\'setContent = new Boxy(
               "<div>';
           $print .= '<label class=\"label_zak\">Unos ocene</label><br /><input type=\"text\" id=\"zak_'.$id.'\" value=\"'.$zak.'\"><br />';
           $print .= '<a id=\"dugme_'.$id.'\" class=\"sb\" href=\"javascript: void(0)\" onclick=\"promeniZakljucnu('.$id.','.$razred_id.','.$grupa_id.'); setContent.unload();\"><span>Unesi</span></a>';
           $print .= '</div>", { title: "Promeni ocenu", closeText: "Zatvori"}); $("#zak_'.$id.'").focus();\'>'.$zak.'</a>';
         }
     }

     $print = "";

     $zak_txt = array('nedovoljan', 'dovoljan', 'dobar', 'vrlodobar', 'odličan');
     $zak_ar = current($zak_ars);

      list($id, $zak) = $zak_ar;
      $opis = '';
      $class = '';
      if($edit)
      {
        $print .= '<a class=\'ocena '.$class.'\' id=\'zak_id_'.$id.'\' href=\'javascript:void(0)\' title=\''.wordwrap($opis, 60, '<br />').'\' onclick=\'setContent = new Boxy(
            "<div>';
        $print .= '<label class=\"label_zak\">Unos ocene</label><br /><input type=\"text\" id=\"zak_'.$id.'\" value=\"'.$zak.'\"><br />';
        $print .= '<a id=\"dugme_'.$id.'\" class=\"sb\" href=\"javascript: void(0)\" onclick=\"promeniZakljucnu('.$id.','.$razred_id.','.$grupa_id.'); setContent.unload();\"><span>Unesi</span></a>';
        $print .= '</div>", { title: "Promeni ocenu", closeText: "Zatvori"}); $("#zak_'.$id.'").focus();\'>'.$zak.'</a>';
      }
      else
      {
        $print .= '<a class=\'ocena '.$class.'\' href=\'javascript:void(0)\' title=\''.wordwrap($opis, 60, '<br />').'\'>'.$zak_txt[$zak - 1].' '.$zak.'</a>';
      }

    return $print;
  }

  public static function novaVladanjeGraf($ucenik_id, $razred_id, $grupa_id, $predmet_id, $predmet_naziv, $period)
  {
      $print = '';
      $print .= '<a id=\'nova_zak_'.$predmet_id.'_'.$period.'\' href=\'javascript:void(0)\' class=\'nova\'
                onclick=\'setContent = new Boxy(
                  "<div>';
      $print .= '<label class=\"label_ocena\">Unos zakljucne ocene</label><br /><input type=\"text\" id=\"ocena_zak_'.$predmet_id.'_'.$period.'\"><br />';
      $print .= '<label class=\"label_ocena\">Upišite obrazloženje za unetu ocenu</label><br /><textarea id=\"opis_zak_'.$predmet_id.'_'.$period.'\" /><br />';
      $print .= '<a id=\"dugme_zak_'.$predmet_id.'_'.$period.'\" class=\"sb\" href=\"javascript: void(0)\" onclick=\"unesiZakljucnuOcenu('.$ucenik_id.','.$razred_id.','.$grupa_id.','.$predmet_id.','.$period.'); setContent.unload();\"><span>Unesi</span></a>';
      $print .= '</div>", { title: "'.$predmet_naziv.'", closeText: "Zatvori"}); $("#ocena_zak_'.$predmet_id.'_'.$period.'").focus();\'>
                <img src=\'/images/unos.gif\' alt=\'nova\' title=\'zakljucna ocena\'></a>';
      return $print;
  }

  public static function novaOcenaGraf($ucenik_id, $razred_id, $grupa_id, $predmet_id, $predmet_naziv, $period)
  {
      $print = '';
      $print .= '<a id=\'nova_'.$predmet_id.'_'.$period.'\' href=\'#\' class=\'nova\'
                onclick=\'setContent = new Boxy(
                  "<div>';
      $print .= '<label class=\"label_ocena\">Unos ocene</label><br /><input type=\"text\" id=\"ocena_'.$predmet_id.'_'.$period.'\"><br />';
      $print .= '<label class=\"label_ocena\">Upišite obrazloženje za unetu ocenu</label><br /><textarea id=\"opis_'.$predmet_id.'_'.$period.'\" /><br />';
      $print .= '<a id=\"dugme_'.$predmet_id.'_'.$period.'\" class=\"sb\" href=\"javascript: void(0)\" onclick=\"unesiOcenu('.$ucenik_id.','.$razred_id.','.$grupa_id.','.$predmet_id.','.$period.'); setContent.unload();\"><span>Unesi</span></a>';
      $print .= '</div>", { title: "'.$predmet_naziv.'", closeText: "Zatvori"}); $("#ocena_'.$predmet_id.'_'.$period.'").focus();\'>
                <img src=\'/images/unos.gif\' alt=\'nova\' title=\'nova ocena\'></a>';
      return $print;
  }

  // OPISNE OCENE
  public static function opisneOceneGraf($ocena_ars, $razred_id, $grupa_id, $edit=0)
  {
    $print = "";
    foreach($ocena_ars as $ocena_ar)
    {
        if(isset($ocena_ar[2]) and $ocena_ar[2] != '')
        {
            list($id, $ocena, $opis) = $ocena_ar;
            $opis_print = mb_substr($opis, 0, 20, 'UTF-8');
            $class = 'ocena_obrazlozena tooltip';
        }
        else
        {
            list($id, $ocena) = $ocena_ar;
            $opis = '';
            $class = '';
        }
          $oc = '';
          if($edit)
          {
            $oc .= '<a class=\'ocena '.$class.'\' id=\'ocena_id_'.$id.'\' href=\'javascript: void(0)\' title=\''.wordwrap($opis, 60, '<br />').'\' onclick=\'setContent = new Boxy(
                  "<div>';
            $oc .= '<label>opis</label><textarea id=\"opis_'.$id.'\">'.$opis.'</textarea><br />';
            $oc .= '<a id=\"dugme_'.$id.'\" class=\"sb\" href=\"javascript: void(0)\" onclick=\"promeniOpisnuOcenu('.$id.','.$razred_id.','.$grupa_id.'); setContent.unload();\"><span>Unesi</span></a>';
            $oc .= '</div>", { title: "Promeni ocenu", closeText: "Zatvori"}); $("#opis_'.$id.'").focus();\'>'.$opis_print.'</a>';
          }
          else
          {
              $oc .= '<a class=\'ocena '.$class.'\' href=\'javascript: void(0)\' title=\''.wordwrap($opis, 60, '<br />').'\'>'.$opis_print.'</a>';
          }

          $print .= $oc;
    }

    return $print;
  }

  public static function novaOpisnaOcenaGraf($ucenik_id, $razred_id, $grupa_id, $predmet_id, $predmet_naziv, $period)
  {
      $print = '';
      $print .= '<a id=\'nova_'.$predmet_id.'_'.$period.'\' href=\'#\' class=\'nova\'
                onclick=\'setContent = new Boxy(
                  "<div>';
      $print .= '<label>opis</label><textarea id=\"opis_'.$predmet_id.'_'.$period.'\" /><br />';
      $print .= '<a id=\"dugme_'.$predmet_id.'_'.$period.'\" class=\"sb\" href=\"javascript: void(0)\" onclick=\"unesiOpisnuOcenu('.$ucenik_id.','.$razred_id.','.$grupa_id.','.$predmet_id.','.$period.'); setContent.unload();\"><span>Unesi</span></a>';
      $print .= '</div>", { title: "'.$predmet_naziv.'", closeText: "Zatvori"}); $("#opis_'.$predmet_id.'_'.$period.'").focus();\'>
                <img src=\'/images/unos.gif\' alt=\'nova\' title=\'nova ocena\'></a>';
      return $print;
  }

  // BROJCANE OCENE
  public static function unesiOcenu(sfWebRequest $request)
  {
      if(!($request->getParameter('ocena') > 0 and $request->getParameter('ocena') < 6))
        return $this->renderText(0);
      $ocena = new Ocena();
      $ocena->setUcenikId($request->getParameter('ucenik_id'));
      $ocena->setRazredId($request->getParameter('razred_id'));
      $ocena->setPredmetId($request->getParameter('predmet_id'));
      $ocena->setOcena($request->getParameter('ocena'));
      $ocena->setOpis($request->getParameter('opis'));
      $ocena->setPeriod($request->getParameter('period'));
      $ocena->save();

      Functions::sendSms($request->getParameter('ucenik_id'), $request->getParameter('ocena'), 'ocena', $request->getParameter('opis'), $request->getParameter('predmet_id'));

      return ($ocena->getId().'_*_'.$request->getParameter('ocena').'_*_'.$request->getParameter('opis'));
  }

  public static function promeniOcenu(sfWebRequest $request)
  {
      if($request->getParameter('grupa_id'))
      {
         if($request->getParameter('ocena') == '')
           DbFinder::from('Ocena')->where('Id', $request->getParameter('ocena_id'))->where('RazredId', $request->getParameter('razred_id'))->delete();
         else if(!($request->getParameter('ocena') > 0 and $request->getParameter('ocena') < 6))
           return 0;
         else
           DbFinder::from('Ocena')->where('Id', $request->getParameter('ocena_id'))->where('RazredId', $request->getParameter('grupa_id'))->set(array('Ocena' => $request->getParameter('ocena'), 'Opis' => $request->getParameter('opis')));
      }
      else
      {
          if($request->getParameter('ocena') == '')
            DbFinder::from('Ocena')->where('Id', $request->getParameter('ocena_id'))->where('RazredId', $request->getParameter('razred_id'))->delete();
          else if(!($request->getParameter('ocena') > 0 and $request->getParameter('ocena') < 6))
            return 0;
          else DbFinder::from('Ocena')->where('Id', $request->getParameter('ocena_id'))->where('RazredId', $request->getParameter('razred_id'))->set(array('Ocena' => $request->getParameter('ocena'), 'Opis' => $request->getParameter('opis')));
      }
      return ($request->getParameter('ocena_id').'_*_'.$request->getParameter('ocena').'_*_'.$request->getParameter('opis'));
  }

  // ZAKLJUCNE OCENE

  public static function unesiZakljucnuOcenu(sfWebRequest $request)
  {
      if(!($request->getParameter('ocena') > 0 and $request->getParameter('ocena') < 6))
        return $this->renderText(0);
       $zakljucna = new Zakljucna();
       $zakljucna->setRazredId($request->getParameter('razred_id'));
       $zakljucna->setPredmetId($request->getParameter('predmet_id'));
       $zakljucna->setPeriod($request->getParameter('period'));
       $zakljucna->setUcenikId($request->getParameter('ucenik_id'));
       $zakljucna->setOcena($request->getParameter('ocena'));
       $zakljucna->setOpis($request->getParameter('opis'));
       $zakljucna->save();

       $zak_txt = array('nedovoljan', 'dovoljan', 'dobar', 'vrlodobar', 'odličan');

       return ($zakljucna->getId().'_*_'.$zak_txt[$request->getParameter('ocena') - 1].' '.$request->getParameter('ocena').'_*_'.$request->getParameter('opis'));
  }

  public static function promeniZakljucnu(sfWebRequest $request)
  {
      if($request->getParameter('grupa_id'))
      {
         if($request->getParameter('ocena') == '')
           DbFinder::from('Zakljucna')->where('Id', $request->getParameter('ocena_id'))->where('RazredId', $request->getParameter('grupa_id'))->delete();
         else if(!($request->getParameter('ocena') > 0 and $request->getParameter('ocena') < 6))
           return 0;
         else
           DbFinder::from('Zakljucna')->where('Id', $request->getParameter('ocena_id'))->where('RazredId', $request->getParameter('grupa_id'))->set(array('Ocena' => $request->getParameter('ocena')));
      }
      else
      {
          if($request->getParameter('ocena') == '')
            DbFinder::from('Zakljucna')->where('Id', $request->getParameter('ocena_id'))->where('RazredId', $request->getParameter('razred_id'))->delete();
          else if(!($request->getParameter('ocena') > 0 and $request->getParameter('ocena') < 6))
            return 0;
          else DbFinder::from('Zakljucna')->where('Id', $request->getParameter('ocena_id'))->where('RazredId', $request->getParameter('razred_id'))->set(array('Ocena' => $request->getParameter('ocena')));
      }
      return ($request->getParameter('ocena_id').'_*_'.$request->getParameter('ocena').'_*_'.'');
  }

  public static function promeniOpisnuZakljucnu(sfWebRequest $request)
  {
      if($request->getParameter('opis') == '')
        DbFinder::from('Zakljucna')->where('Id', $request->getParameter('ocena_id'))->where('RazredId', $request->getParameter('razred_id'))->delete();
      DbFinder::from('Zakljucna')->where('Id', $request->getParameter('ocena_id'))->where('RazredId', $request->getParameter('razred_id'))->set(array('Ocena' => $request->getParameter('ocena'), 'Opis' => $request->getParameter('opis')));
      return ($request->getParameter('ocena_id').'_*_'.$request->getParameter('ocena').'_*_'.$request->getParameter('opis'));
  }

  // OPISNE OCENE
  public static function unesiOpisnuOcenu(sfWebRequest $request)
  {
      if(!($request->getParameter('opis') != ''))
        return $this->renderText(0);
      $ocena = new Ocena();
      $ocena->setUcenikId($request->getParameter('ucenik_id'));
      $ocena->setRazredId($request->getParameter('razred_id'));
      $ocena->setPredmetId($request->getParameter('predmet_id'));
      //$ocena->setOcena($request->getParameter('ocena'));
      $ocena->setOpis($request->getParameter('opis'));
      $ocena->setPeriod($request->getParameter('period'));
      $ocena->save();

      Functions::sendSms($request->getParameter('ucenik_id'), $request->getParameter('ocena'), 'opisna', $request->getParameter('opis'), $request->getParameter('predmet_id'));

      return ($ocena->getId().'_*_'.$request->getParameter('ocena').'_*_'.$request->getParameter('opis'));
  }

  public static function promeniOpisnuOcenu(sfWebRequest $request)
  {
      if($request->getParameter('opis') == '')
        DbFinder::from('Ocena')->where('Id', $request->getParameter('ocena_id'))->where('RazredId', $request->getParameter('razred_id'))->delete();
      DbFinder::from('Ocena')->where('Id', $request->getParameter('ocena_id'))->where('RazredId', $request->getParameter('razred_id'))->set(array('Opis' => $request->getParameter('opis')));
      return ($request->getParameter('ocena_id').'_*_'.$request->getParameter('ocena').'_*_'.$request->getParameter('opis'));
  }

  public static function dnevnikUpisan($cas, $razred_id)
  {
      /*$text = '<div style="">';
      $text .= '<div style="border-bottom: 1px solid #898989; color: #898989; padding: 2px 0; font-weight: 600;">'.$cas['Predmet.PredmetKratki'].'<img onclick="brisiCas(this.id)" id="brisi_'.$cas['Cas.Id'].'" src="/images/x.gif" style="border: 0px; width: 12px; margin-left: 5px;" title="Izbriši upisan čas" class="tooltip" alt="Briši" />'.'</div>';
      $text .= '<div style="min-height: 138px; text-align: left; margin-left: 3px; font-size: 12px;">'.wordwrap(substr($cas['Cas.Opis'], 0, 100), 27, "<br />\n", true).'</div>';
      $text .= '<div style="font-size:12px; width: 27%; display: inline; float: left; text-align: center; border-top: 1px solid #898989; color: #898989; border-right: 1px solid;">'.$cas['Cas.RedBr'].'</div>';
      $text .= self::prikaziDomaci($cas, $razred_id);
      $text .= '</div>';*/

      $text = '<table style="height: 180px">';
      $text .= '<tr>';
      $text .= '<td colspan="3" style="border-bottom: 1px solid #898989; color: #898989; padding: 2px 0; font-weight: 600;">'.$cas['Predmet.PredmetKratki'].'</td>';
      $text .= '</tr>';
      $text .= '<tr>';
      $text .= '<td style="font-size:12px; width: 27%; text-align: center; border-top: 1px solid #898989; color: #898989; border-right: 1px solid;">'.$cas['Cas.RedBr'].'</td>';
      $text .= self::prikaziDomaci($cas, $razred_id);
      $text .= '<td><img onclick="brisiCas(this.id)" id="brisi_'.$cas['Cas.Id'].'" src="/images/x.gif" style="border: 0px; width: 12px; margin-left: 5px;" title="Izbriši upisan čas" class="tooltip" alt="Briši" /></td>';
      $text .= '</tr>';
      $text .= '<tr>';
      $text .= '<td colspan="3" style="height: 138px; text-align: left; margin-left: 3px; font-size: 12px; border-top: 1px solid #898989;">'.wordwrap(substr($cas['Cas.Opis'], 0, 100), 27, "<br />\n", true).'</td>';
      $text .= '</tr>';
      $text .= '</table>';

      return $text;
  }

  public static function dnevnikPrazan($dan, $cas, $rasporeds, $predmets, $plans = 0, $max_red_brs = 0)
  {
      sfContext::getInstance()->getConfiguration()->loadHelpers(array('Form', 'Tag'));
      /*$text = '<div style="min-height: 155px;">';
      $predmet_id = $rasporeds[$dan][$cas];
      $razred_id = sfContext::getInstance()->getRequest()->getParameter('razred_id');
      $text .= select_tag('pre_'.$dan.'_'.$cas, options_for_select($predmets, $predmet_id), array("class" => "dnevnik_predmet", "onchange" => "planoviRadaChange(this.value, this.id, $razred_id)"));

      $text .= '<div style="min-height: 140px;">';

      $text .= '<div class="plan_holder" id="ph_'.$dan.'_'.$cas.'">';
      if(isset($plans[$predmet_id]))
      {
         $plan_ar = $plans[$predmet_id];
         if(isset($max_red_brs[$predmet_id]))
           $max_cas = $max_red_brs[$predmet_id];
         $text .= '<select id="pla_'.$dan.'_'.$cas.'" name="pla_'.$dan.'_'.$cas.'" class="dnevnik_predmet" onchange="planoviRadaIzbor(this.id, this.value)">';
         foreach($plan_ar as $rbr => $plan)
         {
            $selected = '';
            if(isset($max_cas))
              if($rbr == ($max_cas + 1))
                $selected = ' selected="selected"';
            $text .= '<option value="'.$rbr.'"'.$selected.'>'.$rbr.' - '.$plan.'</option>';
         }
         $text .= '</select>';
      }
      $text .= '</div>';

      $text .= '<textarea rows="" cols="" class="cas_text" id="txt_'.$dan.'_'.$cas.'">';
      if(isset($plan_ar))
      {
         if(isset($max_cas))
           $text .= $plan_ar[($max_cas + 1)];
         else  $text .= $plan_ar[1];
      }
      $text .= '</textarea></div>';
      $text .= '<a style="margin-left: 40px;" id="cas_'.$dan.'_'.$cas.'" class="sb" href="javascript: void(0)" onclick="unesiCas(this.id)"><span>Unesi</span></a>';
      $text .= '</div>';*/

      $text = '<div style="min-height: 155px;">';
      $predmet_id = $rasporeds[$dan][$cas];
      $razred_id = sfContext::getInstance()->getRequest()->getParameter('razred_id');
      $text .= select_tag('pre_'.$dan.'_'.$cas, options_for_select($predmets, $predmet_id), array("class" => "dnevnik_predmet", "onchange" => "planoviRadaChange(this.value, this.id, $razred_id)"));

      $text .= '<div style="min-height: 140px;">';

      $text .= '<div class="plan_holder" id="ph_'.$dan.'_'.$cas.'">';
      if(isset($plans[$predmet_id]))
      {
         $plan_ar = $plans[$predmet_id];
         if(isset($max_red_brs[$predmet_id]))
           $max_cas = $max_red_brs[$predmet_id];
         $text .= '<select id="pla_'.$dan.'_'.$cas.'" name="pla_'.$dan.'_'.$cas.'" class="dnevnik_predmet" onchange="planoviRadaIzbor(this.id, this.value)">';
         foreach($plan_ar as $rbr => $plan)
         {
            $selected = '';
            if(isset($max_cas))
              if($rbr == ($max_cas + 1))
                $selected = ' selected="selected"';
            $text .= '<option value="'.$rbr.'"'.$selected.'>'.$rbr.' - '.$plan.'</option>';
         }
         $text .= '</select>';
      }
      $text .= '</div>';

      $text .= '<textarea rows="" cols="" class="cas_text" id="txt_'.$dan.'_'.$cas.'">';
      if(isset($plan_ar))
      {
         if(isset($max_cas))
           $text .= $plan_ar[($max_cas + 1)];
         else  $text .= $plan_ar[1];
      }
      $text .= '</textarea></div>';
      $text .= '<a style="margin-left: 40px; margin-bottom: 3px;" id="cas_'.$dan.'_'.$cas.'" class="sb" href="javascript: void(0)" onclick="unesiCas(this.id)"><span>Unesi</span></a>';
      $text .= '</div>';

      return $text;
  }

  // DNEVNIK RADA
  // Unos casova
  public static function unesiCas(sfWebRequest $request)
  {
      $cas = new Cas();
      $cas->setCas($request->getParameter('cas'));
      $cas->setDatum($request->getParameter('datum'));
      $cas->setOpis($request->getParameter('opis'));
      $cas->setPredmetId($request->getParameter('predmet_id'));
      $cas->setRazredId($request->getParameter('razred_id'));
      $cas->save();

      $cas = DbFinder::from('Cas')->with('Domaci')->leftJoin('Domaci')->with('Predmet')->leftJoin('Predmet')->select(array('Cas.Id', 'Cas.RazredId', 'Cas.PredmetId', 'Cas.Datum', 'Cas.Cas', 'Cas.RedBr', 'Cas.Opis', 'Domaci.Id', 'Domaci.Opis', 'Predmet.PredmetKratki'))->findPk($cas->getId());

      return (self::dnevnikUpisan($cas, $cas['Cas.RazredId']));
  }

  public static function brisiCas(sfWebRequest $request)
  {
      $cas_obj = DbFinder::from('Cas')->where('RazredId', $request->getParameter('razred_id'))->where('Id', $request->getParameter('cas_id'))->findOne();
      $dan = date('N', strtotime($cas_obj->getDatum()));
      $cas = $cas_obj->getCas();
      // Raspored casova
      $tip = 1;
      $rasporeds = array();
      $raspored_arrs = DbFinder::from('Raspored')->select(array('CasP', 'Cas', 'Cas1', 'Cas2', 'Cas3', 'Cas4', 'Cas5', 'Cas6', 'Cas7', 'Cas8', 'Dan'))->where('RazredId', $cas_obj->getRazredId())->where('Tip', $tip)->find();
      foreach($raspored_arrs as $raspored_arr)
      {
          $i = -2;
          while($i++ < 8)
          {
            if($i == -1)
              $i_char = "P";
            else if($i == 0)
              $i_char = "";
            else
              $i_char = $i;

            $rasporeds[$raspored_arr['Dan']][$i] = $raspored_arr['Cas'.$i_char];
          }
      }

      $predmets = $cas_obj->getRazred()->getPredmetFulls(0, 0);
      $predmets[0] = 'Predmet';
      $cas_obj->delete();

      return 'cell_'.$dan.'_'.$cas.'-*-'.self::dnevnikPrazan($dan, $cas, $rasporeds, $predmets);
  }

  // Prikaz domacih
  public static function prikaziDomaci($cas, $razred_id, $edit=0)
  {
      $text = '';
      if(!$edit)
        $text .= '<td id="domaci_cell_'.$cas['Cas.Id'].'" style="width: 50%; display: inline; float: left; text-align: center;">';
      $text .= '<img onclick=\'setContent = new Boxy(
                  "<div>';
      $text .= '<label class=\"label_ocena\">unesite test domaćeg zadatka</label><br /><textarea id=\"opis_'.$cas['Cas.Id'].'\">'.$cas['Domaci.Opis'].'</textarea><br />';
      $text .= '<a id=\"dugme_'.$cas['Cas.Id'].'\" class=\"sb\" href=\"javascript: void(0)\" onclick=\"unesiDomaci('.$cas['Cas.Id'].','.$razred_id.'); setContent.unload();\"><span>Unesi</span></a>';
      $text .= '</div>", { title: "Domaći zadatak", closeText: "Zatvori"}); $("#opis_'.$cas['Cas.Id'].'").focus();\' class="domaci tooltip" title="'.wordwrap($cas['Domaci.Opis'], 80, '<br />').'" style="border: 0; width: 65px;" src="/images/domaci.gif" alt="domaci" />';
      if(!$edit)
        $text .= '</td>';

      return $text;
  }
  // Unos domacih
  public static function unesiDomaci(sfWebRequest $request)
  {
      $cas = DbFinder::from('Cas')->with('Domaci')->leftJoin('Domaci')->select(array('Cas.Id', 'Cas.RazredId', 'Cas.PredmetId', 'Cas.Datum', 'Cas.Cas', 'Cas.RedBr', 'Cas.Opis', 'Domaci.Id', 'Domaci.Opis', 'Predmet.PredmetKratki'))->findPk($request->getParameter('cas_id'));
      if($cas['Domaci.Opis'] != '')
        DbFinder::from('Domaci')->where('CasId', $cas['Cas.Id'])->set(array('Opis' => $request->getParameter('opis')));
      else
      {
        $domaci = new Domaci();
        $domaci->setCasId($cas['Cas.Id']);
        $domaci->setRazredId($cas['Cas.RazredId']);
        $domaci->setPredmetId($cas['Cas.PredmetId']);
        $domaci->setDatum($cas['Cas.Datum']);
        $domaci->setOpis($request->getParameter('opis'));
        $domaci->save();
      }

      $razred = DbFinder::from('Razred')->findPk($cas['Cas.RazredId']);
      $uceniks = $razred->getUcenikIds();
      foreach($uceniks as $ucenik_id)
        Functions::sendSms($ucenik_id, $request->getParameter('opis'), 'domaci', '', $cas['Cas.PredmetId']);

      $cas['Domaci.Opis'] = $request->getParameter('opis');
      return self::prikaziDomaci($cas, $cas['Cas.RazredId'], 1);
  }

  // Izostanci
  public static function noviIzostanak($dan, $cas, $ucenik_id)
  {
      $text = '<a id="izo_'.$ucenik_id.'_'.$dan.'_'.$cas.'" class="novi_izostanak" onclick=unesiIzostanak(this.id)>'.$cas.'</a>';

      return $text;
  }

  public static function unesiIzostanak(sfWebRequest $request)
  {
      if(!DbFinder::from('Izostanak')->where('UcenikId', $request->getParameter('ucenik_id'))->where('Datum', $request->getParameter('datum'))->where('Cas', $request->getParameter('cas'))->count())
      {
         $izostanak = new Izostanak();
         $cas = DbFinder::from('Cas')->where('RazredId', $request->getParameter('razred_id'))->where('Datum', $request->getParameter('datum'))->where('Cas', $request->getParameter('cas'))->findOne();

         if($cas)
            $izostanak->setPredmetId($cas->getPredmetId());
         $izostanak->setCas($request->getParameter('cas'));
         $izostanak->setDatum($request->getParameter('datum'));
         $izostanak->setRazredId($request->getParameter('razred_id'));
         $izostanak->setUcenikId($request->getParameter('ucenik_id'));
         $izostanak->save();

         Functions::sendSms($request->getParameter('ucenik_id'), $request->getParameter('cas'), 'izostanak', '', ($cas) ? $cas->getPredmetId() : null);
      }

      return (self::izostanakUpisan($izostanak, $request->getParameter('dan'), $request->getParameter('cas')));
  }

  public static function izostanakUpisan($izostanak, $dan, $cas)
  {
      if($izostanak->getOpravdan())
        $text = '<a id="izoupisan_'.$izostanak->getUcenikId().'_'.$dan.'_'.$cas.'" class="upisan_izostanak" onclick="brisiIzostanak(this.id,'.$izostanak->getId().')">'.$izostanak->getCas().'</a>';
      else
        $text = '<a id="izoupisan_'.$izostanak->getUcenikId().'_'.$dan.'_'.$cas.'" class="upisan_izostanak neopravdan" onclick="pravdajIzostanak(this.id,'.$izostanak->getId().')">'.$izostanak->getCas().'</a>';

      return $text;
  }

  public static function pravdajIzostanak(sfWebRequest $request)
  {
      $izostanak = DbFinder::from('Izostanak')->where('RazredId', $request->getParameter('razred_id'))->where('Id', $request->getParameter('izostanak_id'))->findOne();
      $izostanak->setOpravdan(1);
      $izostanak->save();

      $text = '<a id="izoupisan_'.$izostanak->getUcenikId().'_'.$request->getParameter('dan').'_'.$request->getParameter('cas').'" class="upisan_izostanak" onclick="brisiIzostanak(this.id,'.$izostanak->getId().')">'.$request->getParameter('cas').'</a>';

      return $text;
  }

  public static function brisiIzostanak(sfWebRequest $request)
  {
      $izostanak = DbFinder::from('Izostanak')->where('RazredId', $request->getParameter('razred_id'))->where('Id', $request->getParameter('izostanak_id'))->findOne();
      $ucenik_id = $izostanak->getUcenikId();
      $izostanak->delete();
      $text = '';
      $text .= 1;
      $text .= '_';
      $text .= $ucenik_id;

      return $text;
  }

}